function pagination(result, total, pagesize) {
    var pageIndex = $("#pageIndex").val();
    var pageNumber = total / pagesize;
    if (pageNumber > 1) {
        pageNumber = Math.ceil(pageNumber);
    } else {
        pageNumber = Math.floor(pageNumber);
    }
    if (pageNumber > 1) {
        var prevstr = "";
        var nextstr = "";
        var pagestr = "";
        if (pageNumber == pageIndex) {
            prevstr = "<a href='javascript:void(0)' onclick='prev()'>&lt;Prev</a>";
            nextstr = "<span>Next&gt;</span>";
        } else if (pageIndex == 1) {
            prevstr = "<span>&lt;Prev</span>";
            nextstr = "<a href='javascript:void(0)' onclick='next()'>Next&gt;</a>";
        } else {
            prevstr = "<a href='javascript:void(0)' onclick='prev()'>&lt;Prev</a>";
            nextstr = "<a href='javascript:void(0)' onclick='next()'>Next&gt;</a>";
        }
        if (pageNumber > 8) {
            var pagetemp = "";
            for (var i = 1; i <= pageNumber; i++) {
                if (i == pageIndex) {
                    pagetemp += "<span>" + i + "</span>";
                } else if (i < pageIndex) {
                    var count = 2;
                    if (pageIndex == pageNumber) {
                        count = 3;
                    }
                    if (pageIndex - i < count) {
                        pagetemp += "<a href='javascript:void(0)' onclick='currentPage(" + i + ")'>" + i + "</a>";
                    }
                } else {
                    var count = 2;
                    if (pageIndex == 1) {
                        count = 3;
                    }
                    if (i - pageIndex < count) {
                        pagetemp += "<a href='javascript:void(0)' onclick='currentPage(" + i + ")'>" + i + "</a>";
                    }
                }
            }
            if (pageIndex <= 3) {
                if (pageIndex == 3) {
                    pagetemp = "<a href='javascript:void(0)' onclick='currentPage(1)'>" + 1 + "</a>" + pagetemp;
                }
                pagestr += pagetemp + "......" + "<a href='javascript:void(0)' onclick='currentPage(" + (pageNumber - 1) + ")'>" + (pageNumber - 1) + "</a>" + "<a href='javascript:void(0)' onclick='currentPage(" + pageNumber + ")'>" + pageNumber + "</a>";
            } else if (pageIndex > pageNumber - 3) {
                if (pageIndex == pageNumber - 2) {
                    pagetemp += "<a href='javascript:void(0)' onclick='currentPage(" + pageNumber + ")'>" + pageNumber + "</a>";
                }
                pagestr += "<a href='javascript:void(0)' onclick='currentPage(1)'>" + 1 + "</a>" + "<a href='javascript:void(0)' onclick='currentPage(2)'>" + 2 + "</a>" + "......" + pagetemp;
            } else {
                pagestr += "<a href='javascript:void(0)' onclick='currentPage(1)'>" + 1 + "</a>" + "..." + pagetemp + "..." + "<a href='javascript:void(0)' onclick='currentPage(" + pageNumber + ")'>" + pageNumber + "</a>";
            }
        } else {
            for (var i = 1; i <= pageNumber; i++) {
                if (i == pageIndex) {
                    pagestr += "<span>" + i + "</span>";
                } else {
                    pagestr += "<a href='javascript:void(0)' onclick='currentPage(" + i + ")'>" + i + "</a>";
                }
            }
        }
        $("#pager").html(prevstr + pagestr + nextstr);
    }

    var sInnerHtml = [];
    for (var i = 0; i < result.length; i++) {
        sInnerHtml.push("<tr>");
        for (var j = 0; j < result[i].length; j++) {
            sInnerHtml.push("<td>");
            sInnerHtml.push(result[i][j]);
            sInnerHtml.push("</td>");
        }
        sInnerHtml.push("</tr>");
    }

    $("#resultList").html(sInnerHtml.join(" "));
    $("#resultList tr:odd").addClass("pinkBg");
    var theadPadding = $("#vendorTableThead").width() - $("#resultsListBox .vendorTableStyle").width() + 4;
    $("#vendorTableThead th:last").css({
        "padding-right": theadPadding
    });
    var vendorTableThead = $("#vendorTableThead tr th");
    var vendorTableBody = $("#resultsListBox tr th");
    for (w = 0; w < vendorTableBody.length; w++) {
        $(vendorTableThead[w]).width($(vendorTableBody[w]).width());
    }
}

function prev() {
    var pageIndex = $("#pageIndex").val();
    $("#pageIndex").val(Number(pageIndex) - Number(1));
    search();
}

function next() {
    var pageIndex = $("#pageIndex").val();
    $("#pageIndex").val(Number(pageIndex) + Number(1));
    search();
}

function currentPage(currentNum) {
    $("#pageIndex").val(currentNum);
    search();
}
